package com.xiaomi.controller;

import com.alibaba.fastjson.JSON;
import com.xiaomi.entity.DiZhi;
import com.xiaomi.model.Result;
import com.xiaomi.service.DiZhiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName DiZhiController
 * @Description TODO
 * @Author lll
 * @Date 2021/11/27 15:53
 * @Version 1.0
 **/
@Controller
@RequestMapping(value = "/DiZhi", produces  = "text/json;charset=UTF-8")
public class DiZhiController {

    @Autowired
    private DiZhiService diZhiService;

    @GetMapping("/pageSearch.do")
    @ResponseBody
    public String pageSearch(@RequestParam Integer page,
                             @RequestParam Integer limit,
                             @RequestParam(required = false) Integer shId,
                             @RequestParam(required = false) String shName) {

        Integer startRow = (page - 1) * limit;

        List<DiZhi> diZhi = diZhiService.pageSearch(startRow, limit, shId, shName);
        Integer count = diZhiService.getCount(shId, shName);
        Result result = new Result();
        result.setCode(0);
        result.setCount(count);
        result.setData(diZhi);

        String s = JSON.toJSONString(result);
        return s;
    }

    @RequestMapping ("/insert.do")
    @ResponseBody
    public String insert(@RequestParam String shName,
                         @RequestParam String sheng,
                         @RequestParam String city,
                         @RequestParam String xiaoqu,
                         @RequestParam String phone) {

        diZhiService.insert(shName, sheng, city, xiaoqu, phone);
        Result result = new Result();
        result.setCode(0);

        String s = JSON.toJSONString(result);
        return s;
    }

    @RequestMapping ("/update.do")
    @ResponseBody
    public String update(@RequestParam String shName,
                         @RequestParam String sheng,
                         @RequestParam String city,
                         @RequestParam String xiaoqu,
                         @RequestParam String phone,
                         @RequestParam Integer shId) {
        diZhiService.update(shName, sheng, city, xiaoqu, phone, shId);
        Result result = new Result();
        result.setCode(0);

        String s = JSON.toJSONString(result);
        return s;
    }

    @RequestMapping ("/delete.do")
    @ResponseBody
    public String delete(@RequestParam Integer shId) {

        diZhiService.delete(shId);
        Result result = new Result();
        result.setCode(0);

        String s = JSON.toJSONString(result);
        return s;
    }
}
